package Base.ProjectTest01.View;

import Base.ProjectTest01.Domain.Employee;
import Base.ProjectTest01.Domain.Programmer;
import Base.ProjectTest01.Service.NameListService;
import Base.ProjectTest01.Service.TeamException;
import Base.ProjectTest01.Service.TeamService;

public class TeamView {
    private NameListService listService = new NameListService();
    private TeamService teamService = new TeamService();

    /**
     * @author 枫城
     * @createTime 2022/10/26 16:06
     * @Description 主页面
     */
    public void enterMainView(){
        Boolean isFinsh = true;
        char c = 0;
        while (isFinsh){
            if (c != '1' ){
                listAllEmployees();
            }
            System.out.print("1-团队列表; 2-添加团队成员; 3-删除团队成员; 4-退出; 请选择(1-4):\t");
            c = TSUtility.readMenuSelection();
            switch (c){
                case '1':
                    getTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    System.out.print("是否退出（Y/N）：");
                    final char yOrNo = TSUtility.readConfirmSelection();
                    if (yOrNo == 'Y'){
                        System.out.println("结束程序");
                        isFinsh = false;
                    }
                    break;
            }
        }

    }

    /**
     * @author 枫城
     * @createTime 2022/10/26 16:07
     * @Description 展示所有成员信息
     */
    public void listAllEmployees(){
        System.out.println("----------------------开发团队调度表-------------------------");
        final Employee[] allEmployees = listService.getAllEmployees();
        System . out. println("ID\t姓名\t\t年龄\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备") ;
        for (Employee employee : allEmployees) {
            System.out.println(employee);
        }
        System.out.println("-----------------------------------------------------------");

    }

    /**
     * @author 枫城
     * @createTime 2022/10/26 16:08
     * @Description 展示在team中的所有成员
     */
    private void getTeam(){
        System.out.println("         ------------------团队成员列表-------------------");

        final Programmer[] team = teamService.getTeam();
        if (team.length ==0 ){
            System.out.println("还没有相关成员");
        }else {
            System . out. println("TID/ID\t\t姓名\t\t年龄\t\t工资\t\t职位\t\t奖金\t\t股票") ;
            for (Programmer programmer : team) {
                System.out.println(programmer.getDetailForTeam());
            }
        }


        System.out.println("-----------------------------------------------------------");
    }


    /**
     * @author 枫城
     * @createTime 2022/10/26 16:09
     * @Description 添加team成员
     */
    private void addMember()  {
        System.out.println("----------------------------添加成员------------------------");
        System.out.print("请输入添加员工的ID：\t");
        int readInt = TSUtility.readInt();
        try{
            final Employee employee = listService.getEmployee(readInt);
            teamService.addMember(employee);
            System.out.println("Successfully added!");
            // 回车键继续
            TSUtility.readReturn();

        }catch (TeamException teamException){
            System.out.println("失败原因："+teamException.getMessage());
        }
    }

    /**
     * @author 枫城
     * @createTime 2022/10/26 16:09
     * @Description 删除指定成员
     */
    public void deleteMember(){
        System.out.println("----------------------------删除成员------------------------");
        System.out.print("请输入需要删除的成员TID：\t");
        final int id = TSUtility.readInt();

        System.out.print("是否确认删除(Y/N):\t");
        final char isDelete = TSUtility.readConfirmSelection();
        if (isDelete == 'N'){
            return;
        }
        try{
            teamService.removeMember(id);
        } catch (TeamException teamException) {
            System.out.println("删除失败，失败原因："+teamException.getMessage());
        }


    }


    /**
     * @desc 程序启动入口
     * @param args
     */
    public static void main(String[] args) {
        final TeamView teamView = new TeamView();
        teamView.enterMainView();
    }

}
